﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MovieRental.EnhancedEntities;
using MovieRental.BuisnessComponents;
using System.Collections.ObjectModel;
using System.Windows.Controls;
using MovieRental_Presentation.Helpers;

namespace MovieRental_Presentation.DataViews
{
    public class CustomersDataView : BaseDataView
    {
        private string _searchString;
        private ObservableCollection<Customer> _customersList = new ObservableCollection<Customer>();
        private ObservableCollection<Customer> _topSpendthrifts = new ObservableCollection<Customer>();

        public CustomersDataView()
        {
            ComponentManager.Instance.CustomerComponent.SourceUpdated += MakeEventHandlerForProperty("CustomersList");
            ComponentManager.Instance.CustomerComponent.SourceUpdated += MakeEventHandlerForProperty("TopSpendthrifts");
            ComponentManager.Instance.LoanCartComponent.SourceUpdated += MakeEventHandlerForProperty("CustomersList");
            ComponentManager.Instance.LoanCartComponent.SourceUpdated += MakeEventHandlerForProperty("TopSpendthrifts");
        }


        public ObservableCollection<Customer> CustomersList
        {
            get
            { 
                var query = ComponentManager.Instance.CustomerComponent.SearchCustomersByName(_searchString).Select(x => new Customer(x));
                UpdateObservableCollection<Customer>(_customersList, query);
                return _customersList;
            }
        }

        public IEnumerable<Customer> TopSpendthrifts
        {
            get 
            { 
                var query = ComponentManager.Instance.CustomerComponent.GetTopSpendthrifts().Select(x => new Customer(x));
                UpdateObservableCollection<Customer>(_topSpendthrifts, query);
                return _topSpendthrifts;
            }
        }

        public string SearchString
        {
            set 
            { 
                SetProperty<string>(ref _searchString, value, "SearchString");
                OnPropertyChanged("CustomersList");
            }
            get
            { return _searchString; }
        }
    }
}
